﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity.Validation;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Datos
{
    [MetadataType(typeof(ProveedorMetaData))]
    public partial class Venta
    {
        Repository<ArqParcial1Entities> repositorio = new Repository<ArqParcial1Entities>();
        ArqParcial1Entities context = new ArqParcial1Entities();

        public IEnumerable<Venta> Listar()
        {
            var result = from venta in context.Ventas
                         select venta;
            return result;
        }

        public Venta Buscar(int iCodigo)
        {
            var result = from venta in context.Ventas
                         where venta.venCodigo == iCodigo
                         select venta;
            return result.Count() > 0 ? result.First() : new Venta();
        }

        public IEnumerable<Venta> Consultar(int? iCodigo, int? iCliente, int? iEmpleado, DateTime? dtFecha)
        {
            var query = from venta in context.Ventas
                        select venta;
            if (iCodigo == null && iCliente == null && iEmpleado== null && dtFecha == null)
            {
                return new List<Venta>();
            }
            else
            {
                if (iCodigo != null)
                {
                    query = query.Where(w => w.venCodigo == iCodigo);
                }
                if (iCliente != null)
                {
                    query = query.Where(w => w.venCliente == iCliente);
                }
                if (iEmpleado != null)
                {
                    query = query.Where(w => w.venEmpleado == iEmpleado);
                }
                if (dtFecha != null)
                {
                    query = query.Where(w => w.venFecha == dtFecha);
                }
                return query;
            }
        }

        public bool Guardar(Venta venta, List<DetalleVenta> detalleventa)
        {
            using (System.Transactions.TransactionScope trn = new System.Transactions.TransactionScope())
            {
                try
                {
                    repositorio.Insert<Venta>(venta);
                    repositorio.Save();
                    foreach (var detalle in detalleventa)
                    {
                        detalle.detvenVenta = venta.venCodigo;
                        repositorio.Insert<DetalleVenta>(detalle);
                        repositorio.Save();
                    }
                    trn.Complete();
                    return true;
                }
                catch (DbEntityValidationException e)
                {
                    foreach (var eve in e.EntityValidationErrors)
                    {
                        Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State);
                        foreach (var ve in eve.ValidationErrors)
                        {
                            Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage);
                        }
                    }
                    return false;

                }
            }
        }

        public bool Guardar(Venta venta)
        {
            using (System.Transactions.TransactionScope trn = new System.Transactions.TransactionScope())
            {
                try
                {
                    repositorio.Insert<Venta>(venta);
                    repositorio.Save();
                    trn.Complete();
                    return true;
                }
                catch (DbEntityValidationException e)
                {
                    foreach (var eve in e.EntityValidationErrors)
                    {
                        Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State);
                        foreach (var ve in eve.ValidationErrors)
                        {
                            Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage);
                        }
                    }
                    return false;

                }
            }
        }
        public bool Actualizar(Venta venta)
        {
            using (System.Transactions.TransactionScope trn = new System.Transactions.TransactionScope())
            {
                try
                {
                    repositorio.Update<Venta>(venta);
                    repositorio.Save();
                    trn.Complete();
                    return true;
                }
                catch (DbEntityValidationException e)
                {
                    foreach (var eve in e.EntityValidationErrors)
                    {
                        Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State);
                        foreach (var ve in eve.ValidationErrors)
                        {
                            Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage);
                        }
                    }
                    return false;

                }
            }
        }
        public bool Eliminar(Venta venta)
        {
            using (System.Transactions.TransactionScope trn = new System.Transactions.TransactionScope())
            {
                try
                {
                    repositorio.Delete<Venta>(venta);
                    repositorio.Save();
                    trn.Complete();
                    return true;
                }
                catch (DbEntityValidationException e)
                {
                    foreach (var eve in e.EntityValidationErrors)
                    {
                        Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State);
                        foreach (var ve in eve.ValidationErrors)
                        {
                            Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage);
                        }
                    }
                    return false;

                }
            }
        }
    }
    class VentaMetaData
    {
    }
}
